package org.springblade.message.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springblade.core.mp.base.BaseEntity;
import org.springblade.message.enums.MessageEnum;
import org.springblade.message.params.MessageParams;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;


/**
 * 消息详情表实体类
 *
 * @author weikun
 * @since 2021-05-08
 */
@Data
@TableName("blade_message_log")
@EqualsAndHashCode(callSuper = true)
@ApiModel(value = "MessageLog发送记录对象", description = "消息记录表")
public class MessageLog extends BaseEntity {

	private static final long serialVersionUID = 1L;

	@ApiModelProperty("主键id")
	@TableId(value = "id", type = IdType.ASSIGN_ID)
	private Long id;

	/**
	 * 标题
	 */
	@ApiModelProperty(value = "标题")
	private String title;

	/**
	 * 内容
	 */
	@ApiModelProperty(value = "内容")
	private String content;

	/**
	 * 类型
	 */
	@ApiModelProperty("类型")
	private Integer type;

	/**
	 * 接收人id
	 */
	@ApiModelProperty(value = "接收人id")
	private Long receiveUser;

	/**
	 * 接收人列表
	 */
	@ApiModelProperty(value = "接收人列表， 逗号分隔数组")
	private String receiveList;

	/**
	 * 接收群id
	 */
	@ApiModelProperty(value = "接收群id 逗号分隔数组")
	private String receiveGroup;


	/**
	 * 接收人名称
	 */
	@TableField(exist = false)
	@ApiModelProperty(value = "接收人名称")
	private String receiveUserName;

	/**
	 * 接收人名称列表
	 */
	@TableField(exist = false)
	@ApiModelProperty(value = "接收人名称列表")
	private List<String> receiverListName;

	/**
	 * 接收部门名称
	 */
	@TableField(exist = false)
	@ApiModelProperty(value = "接收部门名称")
	private List<String> receiveGroupName;

	/**
	 * 接收人id集合
	 */
	@TableField(exist = false)
	@ApiModelProperty(value = "接收人id集合")
	private List<Long> receiveIdList;

	/**
	 * 接收部门id集合
	 */
	@TableField(exist = false)
	@ApiModelProperty(value = "接收部门id集合")
	private List<Long> receiveGroupList;

	public MessageLog(){}


	public MessageLog(MessageParams messageParams) {
		this.title = messageParams.getTitle();
		this.content = messageParams.getContent();
		this.type = messageParams.getType();
		//群消息记录数据库寸逗号分隔字符串
		if (messageParams.getType().equals(MessageEnum.GROUP.getCode())
			|| messageParams.getType().equals(MessageEnum.TREE_GROUP.getCode())) {
			this.receiveGroup = StringUtils.arrayToDelimitedString(messageParams.getReceiveGroupList().toArray(), ",");
			this.receiveGroupList = messageParams.getReceiveGroupList();
		}else if (messageParams.getType().equals(MessageEnum.MULTIPLE.getCode())){
			this.receiveList = StringUtils.arrayToDelimitedString(messageParams.getReceiveIdList().toArray(), ",");
			this.receiveIdList = messageParams.getReceiveIdList();
		} else{
			this.receiveUser = messageParams.getReceiveUser();
		}
		super.setCreateTime(new Date());
	}
}
